
#ifndef _MEMBER_BOBJECT_H_
#define _MEMBER_BOBJECT_H_

#include <BMap>
#include <BObject>

#ifdef _LINUX_
#include <pthread.h>
#else
#include <Windows.h>
#include <WinBase.h>
#undef min
#undef max
#undef far
#undef near
#endif

#define object_member(object)	((member_BObject*)((BObject*)object)->_ptr)

namespace BWE
{
	class SigProc
	{
	public:
		SigProc()
		{
			object = 0;
			method = 0;
		}
		SigProc(BObject* object, BObject::proc method)
		{
			this->object = object;
			this->method = method;
		}
		SigProc(const SigProc& other)
		{
			object = other.object;
			method = other.method;
		}
		inline bool operator == (const SigProc& other) const
		{
			return object == other.object && method == other.method;
		}
		inline bool operator == (const BObject* obj) const
		{
			return object == obj;
		}
		void operator()(BObject* obj, const BValue& val)
		{
			if (object->able())
			{
				(object->*method)(obj, val);
			}
		}

		BObject*		object;
		BObject::proc	method;
	};
	typedef BArray<SigProc>					SigProcArray;
	typedef BMap<int, SigProcArray>			SigProcMap;
	typedef BMap<BString, SigProcArray>		StrSigProcMap;

	class member_BObject
	{
	public:
		member_BObject(BObject* object);
		~member_BObject();
		
		SigProcMap& globalSigProcMap();
		StrSigProcMap& globalStrSigProcMap();

		bool initMutex();

		void emit(int signal, const BValue& value = BValue());

		BObject*		boss;
		BString			name;
		bool			able;
		SigProcMap		sigProcMap;
		StrSigProcMap	strSigProcMap;

		bool		mutex_initialized;

#ifdef _LINUX_
		pthread_mutex_t		mutex;
#else
		CRITICAL_SECTION	criticalSection;
#endif
	};
}

#endif
